package com.tang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.reggie.common.Constant;
import com.tang.reggie.dto.DishDto;
import com.tang.reggie.dto.SetmealDto;
import com.tang.reggie.exception.CustomException;
import com.tang.reggie.mapper.SetmealMapper;
import com.tang.reggie.pojo.*;
import com.tang.reggie.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    /**
     * 添加
     * @param setmealDto
     */
    @Override
    public void saveWithSetmealDish(SetmealDto setmealDto) {
        //添加基本信息到套餐表中
        super.save(setmealDto);

        //添加套餐菜品表数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> select(int page, int pageSize, String name) {

        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        //添加条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)){
            name=name.trim();
            lqw.like(name!="",Setmeal::getName,name);
        }
        lqw.orderByDesc(Setmeal::getUpdateTime);

        super.page(setmealPage,lqw);//按分页及条件查询

        //拷贝基本分页数据
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        //获取到setmealList数据
        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);//将setmeal里的信息拷贝到dto中
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category!=null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(setmealDtoList);
        return setmealDtoPage;
    }

    /**
     * 根据id进行查询,查询所有的信息
     * @param id
     * @return
     */
    @Override
    public SetmealDto selectById(Long id) {
        //查询出套餐基本信息
        Setmeal setmeal = super.getById(id);

        //根据id查询出套餐菜品基本信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(lqw);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    /**
     * 修改
     * @param setmealDto
     */
    @Override
    public void updateWithSetMealDto(SetmealDto setmealDto) {
        //修改套餐基本信息
        super.updateById(setmealDto);

        //清除掉当前套餐类菜品信息
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.eq(setmealDto.getId()!=null, SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(sdlqw);

        //添加新的套餐菜品信息
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        setmealDishService.saveBatch(setmealDishList);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    public void delete(List<Long> ids) {
        //判断是否为套餐中是否存在启用的菜品，存在则不可删除
        //select count(*) from setmeal where id in(1,2,3) and status = 1
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, Constant.ENNABLE_CODE);
        int count = super.count(setmealLambdaQueryWrapper);
        if (count > 0){
            throw new CustomException("套餐中存在启用菜品，请禁用后再删除");
        }

        //先删除套餐菜品关系表
        LambdaQueryWrapper<SetmealDish> sdlqw = new LambdaQueryWrapper<>();
        sdlqw.in(ids!=null,SetmealDish::getSetmealId,ids);
        setmealDishService.remove(sdlqw);

        //删除套餐表基本信息
        super.removeByIds(ids);
    }

    /**
     * 状态修改
     * @param status
     * @param ids
     */
    @Override
    public void updateByStatus(int status, List<Long> ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids!=null,Setmeal::getId,ids);
        super.update(setmeal,lqw);
    }

    @Override
    public List<Setmeal> findByCategory(Setmeal setmeal) {
        //根据套餐id查询套餐信息
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealLambdaQueryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = super.list(setmealLambdaQueryWrapper);

        return list;
    }

    /**
     * 根据套餐id查询出套餐的菜品信息
     * @param id
     * @return
     */
    @Override
    public List<DishDto> listDishs(Long id) {
        //先查询出套餐的菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
        List<DishDto> dishDtoList = setmealDishList.stream().map(setmealDish -> {
            DishDto dishDto = new DishDto();
            dishDto=dishService.selectById(setmealDish.getDishId());
            dishDto.setCopies(setmealDish.getCopies());
            return dishDto;
        }).collect(Collectors.toList());

        return dishDtoList;
    }
}
